package com.zelin.seckill.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.zelin.entity.SeckillGoodsEntity;
import com.zelin.seckill.service.SecKillService;
import com.zelin.utils.MyDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/8/19-12:39
 * ------------------------------
 */
@Service
public class SecKillServiceImpl implements SecKillService {

    public static void main(String[] args) {
        String dateStr = "SeckillGoods_" + MyDate.getDateStr(new Date());
        System.out.println("dateStr = " + dateStr);
        // SeckillGoods_2021081912
    }
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 功能: 根据商品的时间标识得到商品列表
     * 参数:
     * 返回值: java.util.List<com.zelin.entity.SeckillGoodsEntity>
     * 时间: 2021/8/19 12:40
     */
    @Override
    public List<SeckillGoodsEntity> findSeckillGoods(String time) {
        BoundHashOperations<String, String, String> hashOperations = redisTemplate.boundHashOps("SeckillGoods_" + time );
        List<String> values = hashOperations.values();
        System.out.println("values = " + values);
        List<SeckillGoodsEntity> collect = values.stream().map(m -> {
            SeckillGoodsEntity seckillGoodsEntity = JSON.parseObject(m, SeckillGoodsEntity.class);
            return seckillGoodsEntity;
        }).collect(Collectors.toList());
        System.out.println("collect = " + collect);
        return collect;
    }

    /**
     * 功能: 展示菜单
     * 参数:
     * 返回值: java.util.List<java.sql.Date>
     * 时间: 2021/8/19 13:13
     */
    @Override
    public List<Date> listMenus() {
        return MyDate.getDateMenus();
    }

    /**
     * 功能: 根据秒杀商品id查询商品
     * 参数:
     * 返回值: com.zelin.entity.SeckillGoodsEntity
     * 时间: 2021/8/19 14:52
     */
    @Override
    public SeckillGoodsEntity findSecKillGoodsById(String seckillId,String time) {
        String seckillGoods = (String) redisTemplate.boundHashOps("SeckillGoods_" + time).get(seckillId);
        return JSON.parseObject(seckillGoods,SeckillGoodsEntity.class);
    }


}
